<?php
/**
 * Created by PhpStorm.
 * User: refar
 * Date: 21-7-22
 * Time: 上午10:47
 */

namespace app\v2\controller;

use think\Db;
use think\Exception;
use think\Log;

class Status extends Common{
    /**
     * showdoc
     * @catalog 接口文档-V2/侧边栏(TableData)
     * @title 待归还
     * @description 待归还接口
     * @method post
     * @param limit 必选 int 所需页的大小
     * @param offset 必选 int (当前页码-1)*pageSize
     * @param search 必选 string 搜索文本的内容;初始化加载为null
     * @url http://domain/ems-api/v2/Status/getPendingReturn
     * @return
     * @remark 根据搜索框内容进行全字段查询
     */
    public function getPendingReturn() {

        try {
            $pageSize = $this->request->param('limit');
            $offset = $this->request->param('offset');
            $search = $this->request->param('search');

            $allData = Db::table('sms_main_engine')->where('model_status', USING)
                    ->where('user_id', $this->loginUser['T'])
                    ->order('fixed_no desc')->select();

            return apiResponse(SUCCESS, '[Status][getPendingReturn] success',
                $this->getKeywordData($search, $allData, $offset, $pageSize));
        } catch (Exception $e) {
            Log::record('[Status][getPendingReturn] error' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }
    /**
     * showdoc
     * @catalog 接口文档-V2/侧边栏(TableData)
     * @title 待分配
     * @description 待分配接口
     * @method post
     * @param limit 必选 int 所需页的大小
     * @param offset 必选 int (当前页码-1)*pageSize
     * @param search 必选 string 搜索文本的内容;初始化加载为null
     * @url http://domain/ems-api/v2/Status/getPendingAssign
     * @return
     * @remark 无
     */
    public function getPendingAssign() {
        try {
            $pageSize = $this->request->param('limit');
            $offset = $this->request->param('offset');
            $search = $this->request->param('search');

            $allData = Db::table('sms_main_engine')->where('model_status', ASSIGNING)
                        ->order('fixed_no desc')->select();

            return apiResponse(SUCCESS, '[Status][getPendingAssign] success',
                $this->getKeywordData($search, $allData, $offset, $pageSize));
        } catch (Exception $e) {
            Log::record('[Status][getPendingAssign] error' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }
    /**
     * showdoc
     * @catalog 接口文档-V2/侧边栏(TableData)
     * @title 销售流程
     * @description 销售流程接口
     * @method post
     * @param limit 必选 int 所需页的大小
     * @param offset 必选 int (当前页码-1)*pageSize
     * @param search 必选 string 搜索文本的内容;初始化加载为null
     * @url http://domain/ems-api/v2/Status/getSale
     * @return
     * @remark
     */
    public function getSale() {
        try {
            $pageSize = $this->request->param('limit');
            $offset = $this->request->param('offset');
            $search = $this->request->param('search');

            $node = Db::table('sms_approve_node')->where('roleId',$this->loginUser['roleId'])
                ->where('depart', $this->loginUser['depart'])
                ->where('flow_name', '销售')->find();

            $allData = Db::table('sms_main_engine')->where('model_status', $node['target_status'])
                ->order('fixed_no desc')->select();

            return apiResponse(SUCCESS, '[Status][getSale] success',
                $this->getKeywordData($search, $allData, $offset, $pageSize));
        } catch (Exception $e) {
            Log::record('[Status][getSale] error' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }
    /**
     * showdoc
     * @catalog 接口文档-V2/侧边栏(TableData)
     * @title 待借出审批
     * @description 待借出审批接口
     * @method post
     * @param limit 必选 int 所需页的大小
     * @param offset 必选 int (当前页码-1)*pageSize
     * @param search 必选 string 搜索文本的内容;初始化加载为null
     * @url http://domain/ems-api/v2/Status/getPendingBorrow
     * @return
     * @remark
     */
    public function getPendingBorrow() {
        try {
            $pageSize = $this->request->param('limit');
            $offset = $this->request->param('offset');
            $search = $this->request->param('search');

            $borrowClassify = json_decode(BORROW_CLASSIFY, true);

            // 只统计自己课下的机子
            $allData = Db::table('sms_main_engine')->where('model_status', BORROW_REVIEW)
                ->whereIn('department', $this->getDepartV2())
                ->whereIn('borrow_classify', $borrowClassify[$this->loginUser['roleId']])
                ->order('fixed_no desc')->select();

            return apiResponse(SUCCESS, '[Status][getPendingBorrow] success',
                $this->getKeywordData($search, $allData, $offset, $pageSize));
        } catch (Exception $e) {
            Log::record('[Status][getPendingBorrow] error' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }
    /**
     * showdoc
     * @catalog 接口文档-V2/侧边栏(TableData)
     * @title 待删除审批
     * @description 待删除审批接口
     * @method post
     * @param limit 必选 int 所需页的大小
     * @param offset 必选 int (当前页码-1)*pageSize
     * @param search 必选 string 搜索文本的内容;初始化加载为null
     * @url http://domain/ems-api/v2/Status/getPendingDelete
     * @return
     * @remark
     */
    public function getPendingDelete() {
        try {
            $pageSize = $this->request->param('limit');
            $offset = $this->request->param('offset');
            $search = $this->request->param('search');

            $deleteClassify = json_decode(DELETE_CLASSIFY, true);

            // 只统计自己课下的机子
            $allData = Db::table('sms_main_engine')->where('model_status', DELETE_REVIEW_V2)
                ->whereIn('department', $this->getDepartV2())
                ->whereIn('borrow_classify', $deleteClassify[$this->loginUser['roleId']])
                ->order('fixed_no desc')->select();

            return apiResponse(SUCCESS, '[Status][getPendingDelete] success',
                $this->getKeywordData($search, $allData, $offset, $pageSize));
        } catch (Exception $e) {
            Log::record('[Status][getPendingDelete] error' . $e->getMessage());
            return apiResponse(ERROR, 'server error');
        }

    }
    private function getKeywordData($search, $allData, $offset, $pageSize) {

        $allData = itemChangeV2($allData, $this->showVisible);
        $column = getColumnsV2('field', $this->showVisible);

        $jsonResult = array();

        if (null != $search) {
            foreach ($allData as $key => $row) {
                $rowExist = false;
                foreach ($column as $value) {
                    // 包含
                    $r = empty($row[$value]) ? '' : $row[$value];
                    if (stristr($r, $search) !== false) {
                        $rowExist = true;
                        break;
                    }
                }
                // 不存在的话删除
                if (!$rowExist) {
                    unset($allData[$key]);
                }
            }
        }

        $jsonResult['total'] = count($allData);
        $jsonResult['rows'] = array_slice($allData, $offset, $pageSize);

        return $jsonResult;
    }

}